package com.samsung.actividades;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.telephony.SmsManager;
import com.samsung.database.Orden;
import com.samsung.database.Parte;
import com.samsung.database.PartesDBAdapter;

public class SendSms extends Activity {
	
	Orden orden;
	private Activity act;
	private PartesDBAdapter partesDBAdapter;
	Cursor partesCursor;
	Parte parteReader;
	String value;
	String celu1=null;
	String celu2=null;
	String celu3=null;
	
	 public SendSms(Activity act) {			
			this.act = act;
		}
	 
    //---sends an SMS message to another device---
    public void sendSMS( Orden o )
    {   
    	orden = o;
    	String phoneNumbers[] = getPhoneNumber();
    	String message = buildMessage();
        PendingIntent pi = PendingIntent.getActivity(act, 0,
            new Intent(act, SendSms.class), 0);
        String phoneNumber;
        
        for (int i = 0; i < phoneNumbers.length ; i++) {
        	phoneNumber = phoneNumbers[i] ;
        	if (phoneNumber == null || phoneNumber.length() < 2){}
        	else{
        	SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, pi, null);
            }			
		}                
    } 
    
   

	private String buildMessage( ){
		
    	String Message = "El usuario de la Orden " +
    	orden.getNumero() + 
    	" solicita los elementos: "; 
    	String parts = null;
    	
    	for (int i = 0; i < orden.getPartes().size(); i++) {
    		parteReader = orden.getPartes().get(i) ;
			String part  = parteReader.getNro_parte();
			if (part== null || part.length() < 2){
				
			}else{
				if (parts== null || parts.length() < 2){
					parts = part ; 		
				}else{
			parts = parts + " , " + part; }
			}
		}
    	Message = Message + parts ;
    	if ( Message.length() >= 140 ){
    		Message = Message.substring(0, 136) + "...";
    	} 
    	return Message;
    }
    
    private String[] getPhoneNumber( ){
    	String[] Message = new String[3] ;
    	//Message[0] = orden.getNumero1();
    	//Message[1] = orden.getNumero2();
    	//Message[3] = orden.getNumero3();
    	int i = 0;
    	
    	partesDBAdapter = new PartesDBAdapter(act);
   		partesDBAdapter.open();	

		
		partesCursor = partesDBAdapter.getCelulares();
		startManagingCursor(partesCursor);
		//if(partesCursor.moveToNext()){
		if(partesCursor.moveToFirst()){	
			celu1 = partesCursor.getString(partesCursor.getColumnIndex(PartesDBAdapter.KEY_NUMERO));
			 Message[i] = celu1; 
			 i = i + 1;
		}
		if(partesCursor.moveToNext()){
			celu2 = partesCursor.getString(partesCursor.getColumnIndex(PartesDBAdapter.KEY_NUMERO));
			Message[i] = celu2;
			i = i + 1;
		}
		if(partesCursor.moveToNext()){
			celu3 = partesCursor.getString(partesCursor.getColumnIndex(PartesDBAdapter.KEY_NUMERO));
			Message[2] = celu3;
			i = i + 1;
		}
    	return Message;
    }

}
